package com.vehicle.controller;


import com.vehicle.bean.DataGridView;
import com.vehicle.bean.ResultDto;
import com.vehicle.bean.UserVo;
import com.vehicle.pojo.Dept;
import com.vehicle.pojo.User;
import com.vehicle.service.DeptService;
import com.vehicle.service.RoleService;
import com.vehicle.service.UserService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController extends BaseController {
    private Logger logger = Logger.getLogger(this.getClass());
    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;

    @ResponseBody
    @GetMapping("/users")
    public DataGridView userlist(UserVo userVo,Integer deptId) {

        Dept dept=new Dept();
        dept.setDeptid(deptId);
        userVo.setDept(dept);
        DataGridView dataGridView = userService.findUsersBypage(userVo);
        return dataGridView;
    }
    @ResponseBody
    @RequestMapping(value = "/selectDept")
    public List<Dept> deptList(){
        return deptService.findAllDept();
    }

    @ResponseBody
    @RequestMapping(value = "/addUser")
    public ResultDto addUser(User user, @RequestParam("file") MultipartFile file, HttpServletRequest request){
       logger.info(user);
        ResultDto resultDto=new ResultDto();
        //获取根路径
        String rootPath=request.getServletContext().getRealPath("/static/img/");
        //获取图片路径
        String imgPath=file.getOriginalFilename();
        //拼接存放路径
        String path=rootPath+imgPath;
        user.setHeadimg("/static/img/"+imgPath);
        try {

            file.transferTo(new File(path));
            userService.addUser(user);
            logger.info("改变后");
            resultDto.setCode(200);
            resultDto.setMsg("添加成功");
            resultDto.setData(user);

        } catch (IOException e) {
            logger.info("失败了");
            resultDto.setCode(-200);
            resultDto.setMsg("添加失败");
            e.printStackTrace();
        }

        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/deleteUser")
    public ResultDto deleteUser(Integer uid){
        ResultDto resultDto=new ResultDto();
        try {
            userService.deleteUser(uid);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            logger.info("失败了");
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    @ResponseBody
    @RequestMapping(value = "/selectRoleByUid/{uid}")
    public DataGridView selectRoleByUid(@PathVariable Integer uid){
        return roleService.findRoleByUid(uid);
    }
    //删除原有的角色并重新分配
    @RequestMapping(value = "/assignRole")
    @ResponseBody
    public ResultDto assignRole(@RequestParam("uid") Integer uid,@RequestParam("rids[]") Integer[] rids){
        ResultDto resultDto=new ResultDto();
        try {
            userService.assignRole(uid,rids);
            resultDto.setCode(200);
            resultDto.setMsg("分配成功");
        } catch (Exception e) {
            resultDto.setCode(-200);
            resultDto.setMsg("分配失败");
        }
        return resultDto;
    }

    //修改用户
    @ResponseBody
    @RequestMapping("/updateUser")
    public ResultDto updateUser(User user,@RequestParam("file") MultipartFile file,HttpServletRequest request){
        String rootPath=request.getServletContext().getRealPath("/static/img/");
        String imgPath = file.getOriginalFilename();
        String path=rootPath+imgPath;
        ResultDto resultDto=new ResultDto();
        long size = file.getSize();
        if (size>0){
            try {
                User userById = userService.findUserById(user.getUid());
                String headimg = userById.getHeadimg();
                if (headimg!=null){
                    String aA=request.getServletContext().getRealPath("/")+headimg;
                    logger.info(aA);
                    File headimgPath=new File(aA);
                    headimgPath.delete();
                }
                user.setHeadimg("/static/img/"+imgPath);
                file.transferTo(new File(path));
                logger.info("我无敌啦"+user);
                userService.updateUser(user);
                resultDto.setMsg("修改成功");
                resultDto.setCode(200);
            } catch (Exception e) {
                resultDto.setMsg("修改失败");
                resultDto.setCode(-200);
                e.printStackTrace();
            }
        }else {
            try {
                userService.updateUser(user);
                resultDto.setMsg("修改成功");
                resultDto.setCode(200);
            } catch (Exception e) {
                resultDto.setMsg("修改失败");
                resultDto.setCode(-200);
                e.printStackTrace();
            }
        }

        return  resultDto;
    }
    //批量删除
    @ResponseBody
    @RequestMapping(value = "/deleteBatch")
    public ResultDto deleteUserBatch(@RequestParam("uids[]")Integer[] uids){
        ResultDto resultDto=new ResultDto();
        try {
            userService.updateAvailable(uids);
            resultDto.setCode(200);
            resultDto.setMsg("删除成功");
        } catch (Exception e) {
            logger.info("失败了");
            resultDto.setCode(-200);
            resultDto.setMsg("删除失败");
            e.printStackTrace();
        }
        return resultDto;
    }
    //启用禁用
    @ResponseBody
    @RequestMapping("/status")
    public void  updateStatus(Integer uid ,Integer available){
        User user=new User();user.setUid(uid);
        user.setAvailable(available);
        logger.info(user);
        userService.updateStatusOneZero(user);
    }
    //修改密码
    @RequestMapping("/pwd")
    @ResponseBody
    public ResultDto pwd(@RequestParam("uname")String uname,@RequestParam("password")String password,@RequestParam("newPsw1") String newPsw1,@RequestParam("newPsw2") String newPsw2){
        User user= userService.findUserByUid(uname, password);
        ResultDto resultDto=new ResultDto();
        try {
            userService.changePwd(user,newPsw1,newPsw2);
            resultDto.setMsg("修改成功");
            resultDto.setCode(200);
        } catch (Exception e) {
            resultDto.setMsg("修改失败");
            resultDto.setCode(-200);
            e.printStackTrace();
        }
        return resultDto;
    }

}
